package ${groupId}.${artifactIdProjectPackageName}.interceptor;


import ${groupId}.${artifactIdProjectPackageName}.scope.LoginInfoRequest;
import ${groupId}.${artifactIdProjectPackageName}.scope.User;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * 登录信息设置拦截器
 */
@Component
public class LoginInfoSetInterceptor implements HandlerInterceptor {

    @Autowired
    LoginInfoRequest loginInfoRequest;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler){
        loginInfoRequest.setReq(request);
        loginInfoRequest.setRes(response);

        //这边设置登录信息
        String header = request.getParameter("authorization");
        User loginInfo;
        if((loginInfo = getLoginInfo(header)) != null){
            loginInfoRequest.setLoginInfo(loginInfo);
        }
        return true;
    }

    private User getLoginInfo(String token){
        if(token != null){
            User loginInfo = new User();
            loginInfo.setName("admin");
            return loginInfo;
        }
        return null;
    }

}
